package com.wx.disparent.mallparent.commons.util;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 刘杰龙
 * @version 1.0
 * @description 基于datatables的分页工具
 * @date 2019/12/26 12:23
 */
public class PageResult {

    private String data; // 需解析的json数据
    private String sEcho = "0";// 记录操作的次数  每次加1
    private String iDisplayStart = "0";// 起始记录
    private String iDisplayLength = "10";// 页面显示数量
    private String search = "";// 页面显示数量
    private String order = "asc";// 排序规则
    private Integer count = 0; //查询出来的数量
    private Page page;

    public PageResult(String data) {
        this.data = data;
        init();
    }



    public void setOrderBy(String colum,String order){
        //  ASC是根据id 正向排序，DESC是反向排序
        this.page.setOrderBy(colum +" "+order);
    }

    public void setOrderBy(String colum){
        //  ASC是根据id 正向排序，DESC是反向排序
        this.page.setOrderBy(colum +" "+this.order);
    }

    private void init(){
        //获取jquery datatable当前配置参数
        JSONArray jsonArray = JSONArray.parseArray(data);
        for (int i = 0; i < jsonArray.size(); i++) {
            try {
                JSONObject jsonObject = (JSONObject) jsonArray.get(i);
                if (jsonObject.get("name").equals("sEcho"))
                    this.sEcho = jsonObject.get("value").toString();
                else if (jsonObject.get("name").equals("iDisplayStart"))
                    this.iDisplayStart = jsonObject.get("value").toString();
                else if (jsonObject.get("name").equals("iDisplayLength"))
                    this.iDisplayLength = jsonObject.get("value").toString();
                else if (jsonObject.get("name").equals("sSearch"))
                    this.search = jsonObject.get("value").toString();
                else if (jsonObject.get("name").equals("sSortDir_0"))
                    this.order = jsonObject.get("value").toString();
            } catch (Exception e) {
                e.printStackTrace();
            }

            int pageNum = Integer.parseInt(this.iDisplayStart);
            int pageSize = Integer.parseInt(this.iDisplayLength);

            //  创建Page对象，将page，limit参数传入，必须位于从数据库查询数据的语句之前，否则不生效
            /*
             * 第一个参数：第几页;
             * 第二个参数：每页获取的条数.
             */
            this.page = PageHelper.startPage(pageNum/pageSize+1, pageSize);
        }
    }

    public String getsEcho() {
        return sEcho;
    }

    public int getCount() {
        return count;
    }

    public String getSearch() {
        return search;
    }

    public String getiDisplayStart() {
        return iDisplayStart;
    }

    public String getiDisplayLength() {
        return iDisplayLength;
    }

    public String getOrder() {
        return order;
    }

    public Page getPage() {
        return page;
    }

    public List getResultList(List list){
        int initEcho = Integer.parseInt(sEcho);
        List temp = new ArrayList<>();

        if (list != null) {
            //为操作次数加1
            initEcho += 1;
            //总记录数
            this.count = list.size();
            int current = this.count - Integer.parseInt(iDisplayStart);

            if (current > Integer.parseInt(iDisplayLength)) {
                for (int i = Integer.parseInt(iDisplayStart); i < (Integer.parseInt(iDisplayStart) + Integer.parseInt(iDisplayLength)); i++) {
                    if (order.equals("asc")) {
                        temp.add(list.get(i));
                    } else if (order.equals("desc")) {
                        temp.add(list.get((Integer.parseInt(iDisplayStart) + Integer.parseInt(iDisplayLength)) - 1 - i));
                    }
                }
            } else if (current > 0) {
                for (int i = Integer.parseInt(iDisplayStart); i < list.size(); i++) {
                    if (order.equals("asc")) {
                        temp.add(list.get(i));
                    } else if (order.equals("desc")) {
                        temp.add(list.get(Integer.parseInt(iDisplayStart) + list.size() - 1 - i));
                    }
                }
            }
        }

        this.sEcho = String.valueOf(initEcho);
        return temp;
    }

}
